package com.yuanqi.dz.dc.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import pnbclient.helper.StringHelper;
import com.yuanqi.dz.dc.adapters.DCReadFile;
import com.yuanqi.dz.dc.info.MainConfig;
import com.yuanqi.dz.dc.info.MerchantConfig;
import com.yuanqi.dz.dc.util.ReadLocalDZFile;
import com.yuanqi.dz.dc.util.TextValidateUtil;
import com.yuanqitech.advancedpower.powerdate.DateUtil;
import com.yuanqitech.advancedpower.powerdate.PowerDateFormat;
import com.yuanqitech.advancedpower.powernet.NetConnection;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class DZSerivceImpl2  implements DZDataInterface{
	private static  String comm="common";

	public DZInfoArray getDZData(String mercharnum, String dzDate) {
		DZInfoArray  dzInfoArray=new DZInfoArray();
		MerchantConfig  config2 =  TextValidateUtil.config.get("dc").get(mercharnum);
		if(null==config2){
			config2=TextValidateUtil.config.get("dc").get(comm);
		}
		MainConfig  main=config2.getMainConfig();
		String source_type=main.getSourceType();
		String  address=null;
		String local_path =main.getLocalPath();
		String fileName=main.getFileName();
		String dz_date_format=main.getDzDateFormat();
		String dz_date_addday=main.getDzDateAddday();
		String ftp_port =null;
		String  username=null;
		String password = null;
		String  remotePath=null ;

		SimpleDateFormat sdf = new SimpleDateFormat(dz_date_format);
		Calendar calendar = Calendar.getInstance();
		if (StringHelper.isEmpty(dzDate)) {
			calendar.add(Calendar.DAY_OF_MONTH, Integer.valueOf(dz_date_addday.toString()));
		} else {
			calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dzDate));
		}
		String filename = fileName.replace("{merchant}", mercharnum).replace("{date}", sdf.format(calendar.getTime()));
		File file=new File(local_path, filename);

		if(null!=source_type && source_type.equals("1")){
			dzInfoArray= new DCReadFile(dzDate).readFile(file, mercharnum, dzDate, config2);
		}else if("2".equals(source_type)){
			boolean download = false;
			int port = StringHelper.isEmpty(ftp_port) ? 0 : Integer.valueOf(ftp_port);
			try {
				download = new NetConnection(address, port, username, password, remotePath).ftpDownload(file);
			} catch (Exception e) {
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("ftp异常");
				return dzInfoArray;
			}
			if (!download) {
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("ftp上未找到对账文件"+file.getName());
			}
			dzInfoArray= new ReadLocalDZFile().ReadFile(file, mercharnum,dzDate);
		}else{
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("未获取到实现方式获取实现方式暂未开放");
		}



		return dzInfoArray;
	}

	public static void main(String[] args) {
		DZInfoArray  dzInfoArray=new DZInfoArray();
		dzInfoArray=	new DZSerivceImpl2().getDZData("821420180624002", "20160816");
		DZInfo[]  d= dzInfoArray.getDzInfo();
		if(null!=d && d.length>0){
		
			for (int i = 0; i < d.length; i++) {
				System.out.println(d[i].toString());
			}
		}

	}

}
